package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no1_10.no5;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public String longestPalindrome(String s) {
        if(s == null || s.length() == 0){
            return "";
        }
        int start = 0, end = 0;
        int len = 0;
        int maxStart = 0, maxEnd = 0;
        while(start < s.length()) {
            if(len > s.length() - start){
                break;
            }
            for (end = start; end < s.length(); end++) {
                if (isPalindrome(s, start, end)) {
                    if (end - start + 1 > len) {
                        len = end - start + 1;
                        maxStart = start;
                        maxEnd = end;
                    }
                }
            }
            start ++;
        }
        return s.substring(maxStart, maxEnd + 1);
    }

    private boolean isPalindrome(String s, int start, int end){
        while(start < end){
            if(s.charAt(start) == s.charAt(end)){
                start ++;
                end --;
                continue;
            }
            return false;
        }
        return true;
    }
}
